<?php
namespace backend\modules\enterprise\controllers;

use common\models\EnterpriseCase;
use common\models\Proposer;
use Yii;
use yii\base\Exception;
use yii\rest\Controller;
use yii\web\BadRequestHttpException;

/**
 * Created by PhpStorm.
 * User: hu yang
 * Date: 2018/5/3
 * Time: 下午3:25
 */
class CaseController extends Controller
{

    public function verbs()
    {
        return [
            'create' => ['post']
        ];
    }

    /**
     * @return mixed
     * @throws \Exception
     */
    public function actionCreate()
    {
        $enterpriseCase = new EnterpriseCase();
        $proposer       = new Proposer();

        $trans = Yii::$app->getDb()->beginTransaction();
        try {

            $enterpriseCase->load(Yii::$app->getRequest()->getBodyParams(), 'case');
            $enterpriseCase->save();

            $proposer->load(Yii::$app->getRequest()->getBodyParams(), 'proposer');
            $proposer->proposer_enterprise_case_id = $enterpriseCase->getPrimaryKey();
            $proposer->save();

            $trans->commit();
        } catch (Exception $e) {
            $trans->rollBack();
            throw new BadRequestHttpException($e->getMessage());
        }

        return $enterpriseCase->enterprise_case_id;
    }
}